package com.jp.vn.main;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jp.vn.Object.Data;

public class AlphaPractiseType2Activity extends Activity {
	TextView score, japantext, latintext;
	Button btnTrue, btnFalse;
	private boolean answer;
	int newScore = 0;
	String[] word;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alpha_practise_type2);

		score = (TextView) findViewById(R.id.txtScore);
		japantext = (TextView) findViewById(R.id.txtJapan);
		latintext = (TextView) findViewById(R.id.txtLatin);
		btnTrue = (Button) findViewById(R.id.btTrue);
		btnFalse = (Button) findViewById(R.id.btFalse);

		word = getRandomText();
		score.setText(this.newScore + "");
		japantext.setText(this.word[0]);
		latintext.setText(this.word[1]);

		btnFalse.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doButtonFalse();
			}
		});

		btnTrue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doButtonTrue();
			}
		});

	}

	public String[] getRandomText() {
		int i = new Random().nextInt(Data.arrayHira.length);
		int j = new Random().nextInt(Data.arrayHira.length);
		this.answer = new Random().nextBoolean();
		String[] arrayOfString;
		if (!this.answer) {
			while (j == i) {
				j = new Random().nextInt(Data.arrayHira.length);
			}
			arrayOfString = new String[2];
			arrayOfString[0] = Data.arrayHira[i];
			arrayOfString[1] = Data.arrayLatin[j];
		} else {
			arrayOfString = new String[2];
			arrayOfString[0] = Data.arrayHira[i];
			arrayOfString[1] = Data.arrayLatin[i];
		}
		return arrayOfString;
	}

	public void doTrueAnswer() {
		this.word = getRandomText();
		this.newScore = 1 + this.newScore;
		score.setText(this.newScore + "");
		japantext.setText(this.word[0]);
		latintext.setText(this.word[1]);
	}

	public void doWrongAnswer() {
		this.word = getRandomText();
		japantext.setText(this.word[0]);
		latintext.setText(this.word[1]);
	}

	public void doButtonFalse() {
		if (this.answer)
			doWrongAnswer();
		else
			doTrueAnswer();
	}

	public void doButtonTrue() {
		if (!this.answer)
			doWrongAnswer();
		else
			doTrueAnswer();
	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		finish();
	}
}
